<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * IncomingCallModel数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-25> SoChishun Added.
 */
class IncomingCallModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tcti_income_nav';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array();

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'export' => array(
            'field' => "siteID as '租户ID',name as '导航名称',trunkName as '中继名称',callerNo as '主叫号码',calledNo as '被叫号码',recState as '录音标识',sayExtension as '播放工号',music as '放音文件',musicLimit as '放音次数',outOper as '导航动作',outParam as '动作参数', outQueue as '动作参数值',outEcEnable as '评价',dateType as '日期类型',beginTime as '开始时间',endTime as '结束时间'",
            'order' => 'siteID',
            'cache' => true,
        ),
        'item_list' => array(
            'field' => 'id,siteID,name,trunkName,callerNo,calledNo,recState,sayExtension,music,musicLimit,outOper,outParam,outQueue,outEcEnable,dateType,beginTime,endTime',
            'order' => 'siteID',
        ),
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回列表
     * @return array
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_rule_list($where) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count('id');
        $list = $this->cache(true)->scope('item_list')->where($where)->order($orderby)->page($page)->select();
        foreach ($list as $row) {
            $row['siteIDText'] = get_siteID_text($row['siteID']);
            $row['recStateText'] = sofn_get_state_text('yes-no', $row['recState']);
            $row['sayExtensionText'] = sofn_get_state_text('yes-no', $row['sayExtension']);
            $row['musicText'] = ltrim($row['music'], 'xcall_');
            $row['outOperText'] = get_outOper_text($row['outOper']);
            $row['outParamText'] = $row['outParam'] . $row['outQueue'];
            $row['outEcEnableText'] = sofn_get_state_text('yes-no', $row['outEcEnable']);
            $row['dateTypeText'] = get_datetype_text($row['dateType']);
            $out[] = $row;
        }
        return array('total' => $total, 'rows' => $out);
    }

}
